package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.TransferClassesPrice;
import com.example.result.Result;
import com.example.service.TransferClassesPriceService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 转班费控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class TransferClassesPriceController {

    @Autowired
    TransferClassesPriceService transferClassesPriceService;

    // ===============================转班费管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加转班费信息
     */
    @RequestMapping(value = "/transferPrices", method = RequestMethod.POST)
    public Result addTransferClassesPrice(@RequestBody String json) {
        // 将JSON数据转换为TransferClassesPrice对象实体
        JSONObject parseObject = JSON.parseObject(json);
        TransferClassesPrice transferClassesPrice = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TransferClassesPrice.class);

        return transferClassesPriceService.addTransferClassesPrice(transferClassesPrice);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新转班费信息
     */
    @RequestMapping(value = "/transferPrices", method = RequestMethod.PUT)
    public Result updateTransferClassesPrice(@RequestBody String json) {
        // 将JSON数据转换为TransferClassesPrice对象实体
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        TransferClassesPrice transferClassesPrice = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TransferClassesPrice.class);
        return transferClassesPriceService.updateTransferClassesPrice(transferClassesPrice);

    }

    /**
     * @param transferPriceId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据转班费编号删除转班费信息
     */
    @RequestMapping(value = "/transferPrices/{transferPriceId}", method = RequestMethod.DELETE)
    public Result deleteTransferClassesPriceById(@PathVariable("transferPriceId") String transferPriceId) {
        return transferClassesPriceService.deleteTransferClassesPriceById(transferPriceId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有转班费信息
     */
    @RequestMapping(value = "/transferPrices", method = RequestMethod.GET)
    public Result queryAllTransferClassesPrice() {
        return transferClassesPriceService.queryAllTransferClassesPrice();

    }

    /**
     * @param transferPriceId
     * @return 以JSON格式返回该转班费信息
     * @describe 根据编号查询转班费信息
     */
    @RequestMapping(value = "/transferPrices/{transferPriceId}", method = RequestMethod.GET)
    public Result queryTransferClassesPriceById(@PathVariable("transferPriceId") String transferPriceId) {
        return transferClassesPriceService.queryTransferClassesPriceById(transferPriceId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加转班费信息
     */
    @RequestMapping(value = "/transferPrices/excels", method = RequestMethod.POST)
    public Result addTransferClassesPriceByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<TransferClassesPrice> list = JSON.parseArray(parseObject.getString("json"), TransferClassesPrice.class);

        System.out.println(list);

        return transferClassesPriceService.addTransferClassesPriceByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据转班费编号批量删除转班费信息
     */
    @RequestMapping(value = "/transferPrices/dels", method = RequestMethod.POST)
    public Result deleteTransferClassesPriceByIds(@RequestBody Map<String, String> Ids) {

        return transferClassesPriceService.deleteTransferClassesPriceByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 转班费信息模糊查询
     */
    @RequestMapping(value = "/transferPrices/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return transferClassesPriceService.serachByInfo(info);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生转班费用信息
     */
    @RequestMapping(value = "/transferPrices/studentAndTransferTuitions", method = RequestMethod.GET)
    public Result queryAllStudentAndTransferClassPriceInfo() {
        return transferClassesPriceService.queryAllStudentAndTransferClassPriceInfo();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生转班且状态已批阅信息
     */
    @RequestMapping(value = "/transferPrices/studentAndTransferClass", method = RequestMethod.GET)
    public Result queryAllStudentAndTransferClassInfo() {
        return transferClassesPriceService.queryAllStudentAndTransferClassInfo();
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询转班费用信息
     */
    @RequestMapping(value = "/transferPrices/transferTuitionByUserId", method = RequestMethod.POST)
    public Result selectTransferTuitionByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return transferClassesPriceService.selectTransferTuitionByUserId(userId);
    }
}
